package cn.iocoder.yudao.framework.mqtt.config;

import lombok.Data;
import org.eclipse.paho.client.mqttv3.*;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@ConfigurationProperties(prefix = "yudao.mqtt")
@Data
public class YudaoMqttConfiguration {
    @Value("${yudao.mqtt.host}")
    private String host;

    @Value("${yudao.mqtt.clientId}")
    private String clientId;

    @Value("${yudao.mqtt.options.userName}")
    private String userName;

    @Value("${yudao.mqtt.options.password}")
    private String password;

    @Value("${yudao.mqtt.options.cleanSession}")
    private boolean cleanSession;

    @Value("${yudao.mqtt.options.timeout}")
    private int timeout;

    @Value("${yudao.mqtt.options.KeepAliveInterval}")
    private int keepAliveInterval;

    @Value("${yudao.mqtt.options.automaticReconnect}")
    private boolean automaticReconnect;

    @Value("${yudao.mqtt.options.connectionTimeout}")
    private int connectionTimeout;

    @Value("${yudao.mqtt.options.maxInflight}")
    private int maxInflight;

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10); // 根据需要设置线程池大小
        taskScheduler.setThreadNamePrefix("mqtt-task-");
        taskScheduler.initialize();
        return taskScheduler;
    }

    @Bean
    public MqttPahoClientFactory mqttClientFactory() {
        DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
        MqttConnectOptions options = new MqttConnectOptions();
        options.setServerURIs(new String[]{host});
        options.setUserName(userName);
        options.setPassword(password.toCharArray());
        options.setCleanSession(cleanSession);
        options.setConnectionTimeout(connectionTimeout);
        options.setKeepAliveInterval(keepAliveInterval);
        options.setAutomaticReconnect(automaticReconnect);
        options.setMaxInflight(maxInflight);

        factory.setConnectionOptions(options);
        return factory;
    }

    public IMqttClient getMqttClient() {
        try {
            IMqttClient client = mqttClientFactory().getClientInstance(getHost(), getClientId());
            client.connect();
            return client;
        } catch (MqttException exception) {
            exception.printStackTrace();
            return null;
        }
    }
}
